package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.mapper.CollectionMapper;
import one.stand.mapper.CompanyMapperExt;
import one.stand.mapper.CompanyRecruitMapper;
import one.stand.model.*;
import one.stand.request.CollectionRequest;
import one.stand.request.CompanyRecruitRequest;
import one.stand.request.CompanyRequest;
import one.stand.response.CollectionResponse;
import one.stand.response.CompanyRecruitResponse;
import one.stand.response.CompanyResponse;
import one.stand.service.GoodsBaseService;
import one.stand.service.GoodsPhotoService;
import one.stand.service.PersonalService;
import one.stand.util.DateUtil;
import one.stand.util.MoneyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class PersonalServiceImpl implements PersonalService {
    @Autowired
    private GoodsPhotoService goodsPhotoService;
    @Autowired
    private GoodsBaseService goodsBaseService;
    /**
     * 用户收藏信息
     */
    @Autowired
    private CollectionMapper collectionMapper;
    /**
     * 公司招聘表
     */
    @Autowired
    private CompanyRecruitMapper companyRecruitMapper;

    @Autowired
    private CompanyMapperExt companyMapperExt;

    @Override
    public ResultModel addCollection(Integer userId, Integer itemType, Integer itemId) {
        CollectionModel collectionModel = new CollectionModel();
        collectionModel.setUserId(userId);
        collectionModel.setItemType(itemType);
        collectionModel.setItemId(itemId);
        collectionModel.setCreateTime(DateUtil.currentDate());
        int r = collectionMapper.insertSelective(collectionModel);
        if (r > 0) {
            return ResultModel.success("保存成功");
        }
        return ResultModel.fail();
    }

    @Override
    public ResultModel delCollection(Integer userId, Integer itemType, Integer itemId) {
        CollectionModel model = new CollectionModel();
        model.setUserId(userId);
        model.setItemType(itemType);
        model.setItemId(itemId);
        int r = collectionMapper.delCollection(model);
        if (r > 0) {
            return ResultModel.success("删除成功");
        }
        return ResultModel.fail();
    }

    @Override
//    @Cacheable(value = RedisCode.REDIS_DB_CACHE, key = "#root.targetClass.toString()+'.'+#root.methodName+':'+#request.toString()+':'+#request.pageSize+':'+#request.pageNum")
    public ResultModel getCollection(CollectionRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        CollectionModel collectionModel = new CollectionModel();
        collectionModel.setUserId(request.getId().intValue());
        collectionModel.setItemType(request.getItemType());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CollectionModel> list = collectionMapper.select(collectionModel);
        if (list == null) {
            return ResultModel.noData();
        }
        List<CollectionResponse> responseList = new ArrayList<>();
        list.forEach(cl -> {
            if (cl.getItemType() == 1) {
                Goods goods = goodsBaseService.get(cl.getItemId());
                cl.setName(goods.getGoodsName());
                cl.setPrice(MoneyUtil.getMoneyInt(goods.getPrice()));
                cl.setPhotos(goodsPhotoService.getPath(cl.getItemId()));
            }
            CollectionResponse collectionResponse = new CollectionResponse();
            BeanUtils.copyProperties(cl, collectionResponse);
            responseList.add(collectionResponse);
        });
        PageInfo<CollectionResponse> pageInfo = new PageInfo<>(responseList);
        return ResultModel.success(pageInfo.getList());
    }

    @Override
    public ResultModel getCompanyAllInfo(CompanyRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        CompanyModel companyModel = new CompanyModel();
        BeanUtils.copyProperties(request, companyModel);
        List<Company> list = companyMapperExt.selectAll(companyModel);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<CompanyResponse> responseList = new ArrayList<>();
        list.forEach(cl -> {
            CompanyResponse companyResponse = new CompanyResponse();
            BeanUtils.copyProperties(cl, companyResponse);
            responseList.add(companyResponse);
        });
        return ResultModel.success(responseList);
    }

    @Override
//    @Cacheable(value = RedisCode.REDIS_DB_CACHE, key = "#root.targetClass.toString()+'.'+#root.methodName+':'+#request.toString()+':'+#request.pageSize+':'+#request.pageNum")
    public ResultModel<List<CompanyRecruitResponse>> getCompanyRecruit(CompanyRecruitRequest request) {
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        CompanyRecruitModel websiteModel = new CompanyRecruitModel();
        BeanUtils.copyProperties(request, websiteModel);
        List<CompanyRecruitModel> list = companyRecruitMapper.select(websiteModel);
        if (list == null) {
            return ResultModel.noData();
        }
        List<CompanyRecruitResponse> responseList = new ArrayList<>();
        list.forEach(cl -> {
            CompanyRecruitResponse companyRecruitResponse = new CompanyRecruitResponse();
            BeanUtils.copyProperties(cl, companyRecruitResponse);
            responseList.add(companyRecruitResponse);
        });
        return ResultModel.success(responseList);
    }
}
